package com.itheima.homework.test2;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/*
在某次考试中，学生的成绩信息如下： 姓名 年龄 成绩 Tom 20 90 Jerry 22 95 John 20 100 Lily 22 100 Lucy 22 90 Kevin 22 90
请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序，如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序，
成绩和年龄都一样，则按照姓名的字典顺序排序。
 */
public class Demo2 {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int res1 = o2.getSorce() - o1.getSorce();
                int res2 = res1 == 0 ? o2.getAge() - o1.getAge() : res1;
                int res3 = res2 == 0 ? o2.getName().compareTo(o1.getName()) : res2;
                return res3;
            }
        });
        Student stu1 = new Student("Tom",20,90);
        Student stu2 = new Student("Jerry",22,95);
        Student stu3 = new Student("John",20,100);
        Student stu4 = new Student("Lily",22,100);
        Student stu5 = new Student("Lucy",22,90);
        Student stu6 = new Student("Kevin",22,90);
        ts.add(stu1);
        ts.add(stu2);
        ts.add(stu3);
        ts.add(stu4);
        ts.add(stu5);
        ts.add(stu6);
        System.out.println(ts);
        }
    }

class Student{
    private String name;
    private int age;
    private int sorce;

    public Student() {
    }

    public Student(String name, int age, int sorce) {
        this.name = name;
        this.age = age;
        this.sorce = sorce;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSorce() {
        return sorce;
    }

    public void setSorce(int sorce) {
        this.sorce = sorce;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sorce=" + sorce +
                '}';
    }
}